package com.jzo2o.customer.controller.consumer;

import com.jzo2o.api.customer.dto.response.AddressBookResDTO;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.customer.model.domain.AddressBook;
import com.jzo2o.customer.model.dto.request.AddressBookPageQueryReqDTO;
import com.jzo2o.customer.model.dto.request.AddressBookUpsertReqDTO;
import com.jzo2o.customer.service.IAddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("consumerAddressBookController")
@RequestMapping("/consumer/address-book")
@Api(tags = "用户端 - 地址簿相关接口")
public class AddressBookController {


    @Autowired
    private IAddressBookService addressBookService;
    /**
     * 添加地址
     * @param dto
     * @return
     */
    @PostMapping
    @ApiOperation("地址新增")
    public void add(@RequestBody AddressBookUpsertReqDTO dto){
        addressBookService.add(dto);

    }
    /**
     * 查询地址
     */
    @GetMapping("/page")
    @ApiOperation("地址簿分页查询请求参数")
    public PageResult<AddressBookResDTO> list(AddressBookPageQueryReqDTO dto) {
        return addressBookService.getpage(dto);
    }
    @GetMapping("/{id}")
    @ApiOperation("地址簿编辑")
    public AddressBookResDTO listgetById(@PathVariable Long id) {
        AddressBook addressBook = addressBookService.getById(id);
        return BeanUtils.copyBean(addressBook, AddressBookResDTO.class);
    }
    @PutMapping("/{id}")
    @ApiOperation("地址簿编辑")
    public void update(@PathVariable Long id ,@RequestBody AddressBookUpsertReqDTO dto) {

        addressBookService.updateById(id,dto);
    }
    @DeleteMapping("/batch")
    @ApiOperation("批量删除")
    public void deleteBatch(@RequestBody List<Long> ids) {
        addressBookService.removeByIds(ids);
    }
    /**
     * 设置默认地址
     * @param id
     * @return
     */
    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public void setDefault(@RequestParam Long id, @RequestParam int flag) {
        addressBookService.setDefault(id, flag);
    }
    /**
     * 获取默认地址
     * @return addressBook
     */
    @GetMapping("/defaultAddress")
    @ApiOperation("获取默认地址")
    public AddressBookResDTO getDefaultAddress() {
        return addressBookService.getDefaultAddress();

    }

}